package com.fairytask.engine.components.tab
{
	import com.fairytask.engine.components.BaseComponent;
	import com.fairytask.engine.components.text.Label;
	import com.fairytask.engine.define.Filter;
	import com.fairytask.managers.ResManager;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	public class TabBarItem extends BaseComponent
	{
		//容器
		private var _bgSpace		:Sprite;
		private var _iconSpace		:Sprite;
		private var _txtLabel		:Label;
		private var _normalMC		:MovieClip;
		private var _overMC			:MovieClip;
		private var _downMC			:MovieClip;
		private var _disableMC		:MovieClip;
		private var _hotArea		:Sprite;
		
		//皮肤
		private var _icon			:DisplayObject;
		
		//设定
		private var _itemWidth		:Number	=	0;
		private var _itemHeight	:Number	=	0;
		private var _currentSkin	:MovieClip;
		
		//数据
		private var _id			:int	=	0;
		private var _selected		:Boolean=	false;
		private var _enabled		:Boolean=	true;
		private var _isEvent		:Boolean=	true;
		
		public function TabBarItem(str:String,w:Number,h:Number,itemSkin:String="")
		{
			super();
			_itemWidth	=	w;
			_itemHeight	=	h;
			
			initGUI(str);
			initEvent();
			skin		=	itemSkin;
			buttonMode	=	true;
		}
		
		private function initGUI(str:String):void
		{
			_label				=	str;
			_txtLabel			=	new Label();
			_txtLabel.labelFilter	=	Filter.BAG_TEXT_FILTER;
			_txtLabel.color		=	"FFFFFF";
			_txtLabel.htmlStrByBorder	=	str;
			_bgSpace			=	new Sprite();
			
			_hotArea			=	new Sprite();
			
			_iconSpace			=	new Sprite();
			
			_txtLabel.mouseEnabled	=	_iconSpace.mouseEnabled	=	false;
			
			addChild(_bgSpace);
			addChild(_txtLabel);
			addChild(_iconSpace);
			
			addChild(_hotArea);
			
			drawBG();
			
			alignTxt();
			
			drawHotArea();
		}
		
		private function initEvent():void
		{
			_isEvent	=	true;
			_hotArea.addEventListener(MouseEvent.ROLL_OVER,showOver);
			_hotArea.addEventListener(MouseEvent.ROLL_OUT,showOut);
			_hotArea.addEventListener(MouseEvent.MOUSE_DOWN,showDown);
			_hotArea.addEventListener(MouseEvent.MOUSE_UP,showUP);
			_hotArea.addEventListener(MouseEvent.CLICK,onClick);
		}
		
		private function drawBG():void
		{
			_bgSpace.graphics.clear();
			_bgSpace.graphics.beginFill(0xff0000,0);
			_bgSpace.graphics.drawRect(0,0,_itemWidth,_itemHeight);
			_bgSpace.graphics.endFill();
		}
		
		private function drawHotArea():void
		{
			_hotArea.graphics.clear();
			_hotArea.graphics.beginFill(0xff0000,0);
			_hotArea.graphics.drawRect(0,0,_itemWidth,_itemHeight);
			_hotArea.graphics.endFill();
		}
		
		private function alignTxt():void
		{
			_txtLabel.x	=	(width-_txtLabel.width)/2;
			_txtLabel.y	=	(height-_txtLabel.height)/2-1;
		}
		
		private function showOver(e:MouseEvent):void
		{
			if(_currentSkin!=_overMC&&!selected)
			{
				clearBG();
				_bgSpace.addChild(_overMC);
				_currentSkin	=	_overMC;
			}
		}
		
		private function onClick(e:MouseEvent):void
		{
			if(enabled)
			{
				selected	=	true;
				if(_currentSkin!=_overMC)
				{
					clearBG();
					_bgSpace.addChild(_overMC);
					_currentSkin	=	_overMC;
				}
				dispatchEvent(new MouseEvent(MouseEvent.CLICK));
			}
		}
		
		private function showOut(e:MouseEvent):void
		{
			if(_currentSkin!=_normalMC&&!selected)
			{
				clearBG();
				_bgSpace.addChild(_normalMC);
				_currentSkin	=	_normalMC;
			}
		}
		
		private function showDown(e:MouseEvent):void
		{
			if(_currentSkin!=_downMC&&!selected)
			{
				clearBG();
				_bgSpace.addChild(_downMC);
				_currentSkin	=	_downMC;
			}
		}
		
		private function showUP(e:MouseEvent):void
		{
			if(_currentSkin!=_overMC&&!selected)
			{
				clearBG();
				_bgSpace.addChild(_overMC);
				_currentSkin	=	_overMC;
			}
		}
		
		private function clearBG():void
		{
			while(_bgSpace.numChildren>0)
			{
				_bgSpace.removeChildAt(0);
			}
		}
		
		override public function set width(val:Number):void
		{
			_itemWidth		=	val;
			drawBG();
			resizeSkin();
			alignTxt();
			drawHotArea();
		}
		override public function set height(val:Number):void
		{
			_itemHeight		=	val;
			drawBG();
			resizeSkin();
			alignTxt();
			drawHotArea();
		}
		
		private function resizeSkin():void
		{
			if(_skin!="")
			{
				_disableMC.width	=	_normalMC.width	=	_overMC.width	=	_downMC.width	=	_itemWidth;
				_disableMC.height	=	_normalMC.height=	_overMC.height	=	_downMC.height	=	_itemHeight;
			}
		}
		
		override public function set skin(value:String):void
		{
			clearBG();
			_skin = value;
			if(_skin=="")
			{
				drawBG();
			}else{
				var res:MovieClip	=	ResManager.newResClass(_skin) as MovieClip;
				_normalMC			=	res.getChildByName("Button_normal") as MovieClip;
				_overMC			=	res.getChildByName("Button_over") as MovieClip;
				_downMC			=	res.getChildByName("Button_down") as MovieClip;
				_disableMC		=	res.getChildByName("Button_unallowed") as MovieClip;
				resizeSkin();
				_disableMC.x	=	_normalMC.x		=	_overMC.x	=	_downMC.x	=	0;
				_disableMC.y	=	_normalMC.y		=	_overMC.y	=	_downMC.y	=	0;
				_bgSpace.addChild(_normalMC);
				_currentSkin	=	_normalMC;
			}
		}
		
		public function set icon(value:DisplayObject):void
		{
			while(_iconSpace.numChildren>0)_iconSpace.removeChildAt(0);
			_icon	=	value;
			if(_icon!=null)
			{
				_iconSpace.addChild(_icon);
			}
		}
		
		public function get id():int
		{
			return _id;
		}
		
		public function set id(value:int):void
		{
			_id = value;
		}
		
		override public function get label():String
		{
			return _label;
		}
		
		override public function set label(value:String):void
		{
			_label = value;
			_txtLabel.htmlStr	=	_label;
			alignTxt();
		}

		public function get selected():Boolean
		{
			return _selected;
		}

		public function set selected(value:Boolean):void
		{
			_selected = value;
			if(!_selected&&_enabled)
			{
				if(_currentSkin!=_normalMC)
				{
					clearBG();
					_bgSpace.addChild(_normalMC);
					_currentSkin	=	_normalMC;
				}
			}
			else if(_selected&&_enabled)
			{
				if(_currentSkin!=_downMC)
				{
					clearBG();
					_bgSpace.addChild(_downMC);
					_currentSkin	=	_downMC;
				}
			}
		}

		override public function get enabled():Boolean
		{
			return _enabled;
		}

		override public function set enabled(value:Boolean):void
		{
			_enabled = value;
			if(!_enabled)
			{//_disableMC
				if(_currentSkin!=_disableMC)
				{
					clearBG();
					_bgSpace.addChild(_disableMC);
					_currentSkin	=	_disableMC;
				}
				_isEvent	=	false;
				_hotArea.removeEventListener(MouseEvent.ROLL_OVER,showOver);
				_hotArea.removeEventListener(MouseEvent.ROLL_OUT,showOut);
				_hotArea.removeEventListener(MouseEvent.MOUSE_DOWN,showDown);
				_hotArea.removeEventListener(MouseEvent.MOUSE_UP,showUP);
				_hotArea.removeEventListener(MouseEvent.CLICK,onClick);
			}else{
				if(_currentSkin!=_normalMC)
				{
					clearBG();
					_bgSpace.addChild(_normalMC);
					_currentSkin	=	_normalMC;
				}
				if(!_isEvent)
				{
					initEvent();
				}
			}
		}


	}
}